package com.lyp.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.jsonbean.LoginBean;
import com.lyp.jsonbean.OpinionBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//意见反馈
public class OpinionActivity extends Activity implements View.OnClickListener{

        private ImageView mBack;
        private EditText mCont;
        private Button mQueren;
        private String mContent;
        private LoginBean.DataBean bean;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_opinion);
            initview();
        }

        void initview(){
            bean = (LoginBean.DataBean) getIntent().getExtras().get("databean");
            mBack = (ImageView) findViewById(R.id.iv_opinion_back);
            mCont = (EditText) findViewById(R.id.tv_content);
            mQueren = (Button) findViewById(R.id.btn_order_confirm);
            mQueren.setVisibility(View.VISIBLE);
            mQueren.setOnClickListener(this);
            mBack.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.iv_opinion_back:
                    finish();
                    break;
                case R.id.btn_order_confirm:
                    try {
                        mContent = mCont.getText().toString();
                        opinion(bean.getId(),mContent);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }


    //反馈
    public void opinion(String a_id,String feedback) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        YangxixiApi github = retrofit.create(YangxixiApi.class);
        Call<OpinionBean> call = github.getOpinion(a_id,feedback);
        call.enqueue(new Callback<OpinionBean>() {
            @Override
            public void onResponse(Call<OpinionBean> call, final Response<OpinionBean> response) {
                if (response.body().getResult()== 1 ) {
                    OpinionActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new MaterialDialog.Builder(OpinionActivity.this)
                                    .content("恭喜你反馈信息成功!")
                                    .positiveText("确定")
                                    .canceledOnTouchOutside(false)
                                    .cancelable(false)
                                    .onPositive(new MaterialDialog.SingleButtonCallback(){
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            finish();
                                        }
                                    })
                                    .show();
                                 }
                         });
                }else {
                    ToastUtils.showShort(response.body().getMessage());
                }
            }
            @Override
            public void onFailure(Call<OpinionBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }
}
